<?php

//小站控制器
class Site extends Controller {

	var $viewpath;
	var $is_logined;
	var $prepage = 20;
	
	function Site()
	{
		parent::Controller();
		$this->viewpath = 'admin/'.__CLASS__;
		$this->load->model('managers_model','mm');
		$this->load->model('site_model','sm');
		$this->is_logined = $this->mm->is_logined();
		
	}
	
	//公共处理：链接构造
	function common()
	{
		//搜索字符处理
		$sortby = 0;
		$desc = "desc";
		$page = 0;
		parse_str($_SERVER["QUERY_STRING"], $this->param);
		$keyword = "";
		if (isset($this->param["keyword"])) {
			$keyword = safeEncoding(trim($this->param["keyword"]));//字符转换处理
		}
		if (isset($this->param["sortby"])) {
			$sortby = $this->param["sortby"];
		}
		if (isset($this->param["desc"])) {
			$desc = $this->param["desc"];
		}
		if (isset($this->param["page"])) {
			$page = $this->param["page"];
		}
		$this->base_url = $this->config->item('admin_path')."/site/index/?keyword=".$keyword."&sortby=".$sortby."&desc=".$desc."&dosubmit=1&page=".$page;	
	}
	
	//默认首页
	function index()
	{
		if(!$this->is_logined) {
			redirect($this->config->item('admin_path')."/main/login");
		} 
		$manager = $this->mm->get_account();	
				
		//搜索字符处理
		$sortby = 0;
		$page = 0;
		$desc = "desc";
		parse_str($_SERVER["QUERY_STRING"], $this->param);
		$keyword = "";
		if (isset($this->param["keyword"])) {
			$keyword = safeEncoding(trim($this->param["keyword"]));//字符转换处理
		}
		if (isset($this->param["sortby"])) {
			$sortby = $this->param["sortby"];
		}
		if (isset($this->param["desc"])) {
			$desc = $this->param["desc"];
		}
		if (isset($this->param["page"])) {
			$page = $this->param["page"];
		}
		
		//获取列表
		(!isset($this->param["page"]) || $this->param["page"] == "") ? $offset = 0 : $offset = $this->param["page"];
		$items = $this->sm->listitem($offset, $this->prepage, $sortby, $desc, $keyword);
		
		//分页数据处理
		$this->load->library('pagination');
		$base_url = site_url($this->config->item('admin_path')."/site/index/?keyword=".$keyword."&sortby=".$sortby."&desc=".$desc."&dosubmit=1");
		$total_rows = $this->sm->countitems($keyword);//+keyword
		$pagestring = pages($base_url,$total_rows,3,$this->prepage, true);//query string
		
		$viewdata['viewpath'] = $this->viewpath;
		$viewdata['manager'] = $manager;
		$viewdata['items'] = $items;
		$viewdata['pagestring'] = $pagestring;
		$viewdata['sortby'] = $sortby;
		$viewdata['desc'] = $desc;
		$viewdata['keyword'] = $keyword;
		$viewdata['total_rows'] = $total_rows;
		$viewdata['page'] = $page;
		$this->load->view($this->viewpath.'/index', $viewdata);
	}
	
	//粉丝
	function fans()
	{
		if(!$this->is_logined) {
			redirect($this->config->item('admin_path')."/main/login");
		} 
		$manager = $this->mm->get_account();	
		$siteid = u(4);
		//获取列表
		$items = $this->ssm->get_funs($siteid, $this->prepage, u(5));
		//分页数据处理
		$this->load->library('pagination');
		$config['base_url'] = site_url($this->config->item('admin_path')."/site/fans/".$siteid);
		$config['total_rows'] = $this->ssm->get_sitefuns($siteid);
		$config['per_page'] = $this->prepage;
		$config['uri_segment'] = 5;
		$config['num_links'] = 1;
		$config['first_link'] = '首页';
		$config['last_link'] = '末页';
		$config['prev_link'] = '上一页';
		$config['next_link'] = '下一页';
		$this->pagination->initialize($config);
		$pagestring = $this->pagination->create_links();
		
		$viewdata['viewpath'] = $this->viewpath;
		$viewdata['manager'] = $manager;
		$viewdata['items'] = $items;
		$viewdata['pagestring'] = $pagestring;
		$viewdata['siteid'] = $siteid;
		$this->load->view($this->viewpath.'/fans', $viewdata);
	}
	
	//小站统计
	function statis()
	{
		if(!$this->is_logined) {
			redirect($this->config->item('admin_path')."/main/login");
		} 
		$manager = $this->mm->get_account();	
		//获取列表
		$items = $this->sm->listitem(u(4), $this->prepage);
		foreach ($items as $key => $item) {
			$siteid = $item['id'];
			//本周新增粉丝数据统计
			$new_weekfuns = $this->ssm->statis_newfuns($siteid);
			//本月新增粉丝数据统计
			$new_monthfuns = $this->ssm->statis_newfuns($siteid, "month");
			//小站统计数据
			$sitestatis = $this->ssm->get_sitestatis($siteid);
			
			$avg_fanstaytime = $avg_visitstaytime = $visits2fans = $avg_visits = 0;
			if(count($sitestatis)>0) {
				//每位粉丝平均停留时间
				if($sitestatis["fans"] > 0) $avg_fanstaytime = ceil($sitestatis["fanstaytime"]/$sitestatis["fans"]);
				if($sitestatis["visits"] > 0) {
					//每日访客
					$days = time() - $item["regtime"]/86400;
					$avg_visits = ceil($sitestatis["visits"]/$days);
				}
			}
			$items[$key]['new_weekfuns'] = $new_weekfuns;
			$items[$key]['new_monthfuns'] = $new_monthfuns;
			$items[$key]['avg_fanstaytime'] = $avg_fanstaytime;
			$items[$key]['avg_visits'] = $avg_visits;
		}
		//分页数据处理
		$this->load->library('pagination');
		$config['base_url'] = site_url($this->config->item('admin_path')."/site/statis");
		$config['total_rows'] = $this->sm->countitems();
		$config['per_page'] = $this->prepage;
		$config['uri_segment'] = 4;
		$config['num_links'] = 1;
		$config['first_link'] = '首页';
		$config['last_link'] = '末页';
		$config['prev_link'] = '上一页';
		$config['next_link'] = '下一页';
		$this->pagination->initialize($config);
		$pagestring = $this->pagination->create_links();
		
		$viewdata['viewpath'] = $this->viewpath;
		$viewdata['manager'] = $manager;
		$viewdata['items'] = $items;
		$viewdata['pagestring'] = $pagestring;
		$this->load->view($this->viewpath.'/data', $viewdata);
	}
	
	//选择审核
	function statusselected()
	{
		$this->common();
		if (isset($this->param["delst"])) {
			$delstr = $this->param["delst"];
		}
		if(!isset($delstr)) redirect($this->config->item('admin_path')."/site/index");
		$this->sm->statusselected($delstr);
		admin_show_success("审核成功", $this->base_url);	
	}
	
	//选择认证
	function certselected()
	{
		$this->common();
		if (isset($this->param["delst"])) {
			$delstr = $this->param["delst"];
		}
		if(!isset($delstr)) redirect($this->config->item('admin_path')."/site/index");
		$this->sm->certselected($delstr);
		admin_show_success("认证成功", $this->base_url);	
	}
	
	//选择推荐
	function recommendselected()
	{
		$this->common();
		if (isset($this->param["delst"])) {
			$delstr = $this->param["delst"];
		}
		if(!isset($delstr)) redirect($this->config->item('admin_path')."/site/index");
		$time = time();
		$this->sm->recommendselected($delstr, $time);
		admin_show_success("推荐成功", $this->base_url);	
	}
	
	//选择看看
	function kankanselected()
	{
		$this->common();
		if (isset($this->param["delst"])) {
			$delstr = $this->param["delst"];
		}
		if(!isset($delstr)) redirect($this->config->item('admin_path')."/site/index");
		$time = time();
		$this->sm->kankanselected($delstr, $time);
//		admin_show_success("推荐成功", $this->config->item('admin_path')."/site/index");	
		admin_show_success("推荐成功", $this->base_url);	
	}
	
	//选择推荐男生/女生
	function recommendselectedstatic()
	{
		$this->common();
		if (isset($this->param["delst"])) {
			$delstr = $this->param["delst"];
		}
		$type = '1';
		if (isset($this->param["type"])) {
			$type = $this->param["type"];
		}
		if(!isset($delstr)) redirect($this->config->item('admin_path')."/site/index");
		$time = time();
		($type == '2') ? $field = "girl" : $field = "boy";
		$this->sm->recommendselectedstatic($delstr, $field, $time);
		admin_show_success("推荐成功", $this->base_url);	
	}
	
	//选择推荐
	function bestrecommendselected()
	{
		$this->common();
		if (isset($this->param["delst"])) {
			$delstr = $this->param["delst"];
		}
		if(!isset($delstr)) redirect($this->config->item('admin_path')."/site/index");
		$time = time();
		$this->sm->bestrecommendselected($delstr, $time);
		admin_show_success("推荐成功", $this->base_url);	
	}
	
	//选择审核
	function unstatusselected()
	{
		$this->common();
		if (isset($this->param["delst"])) {
			$delstr = $this->param["delst"];
		}
		if(!isset($delstr)) redirect($this->config->item('admin_path')."/site/index");
		$this->sm->statusselected($delstr, 0);
		admin_show_success("取消审核成功", $this->base_url);	
	}
	
	//选择认证
	function uncertselected()
	{
		$this->common();
		if (isset($this->param["delst"])) {
			$delstr = $this->param["delst"];
		}
		if(!isset($delstr)) redirect($this->config->item('admin_path')."/site/index");
		$this->sm->certselected($delstr, 0);
		admin_show_success("取消认证成功", $this->base_url);	
	}
	
	//选择取消推荐
	function unrecommendselected()
	{
		$this->common();
		if (isset($this->param["delst"])) {
			$delstr = $this->param["delst"];
		}
		if(!isset($delstr)) redirect($this->config->item('admin_path')."/site/index");
		$this->sm->recommendselected($delstr, 0);
		admin_show_success("取消推荐成功", $this->base_url);	
	}
	
	//选择取消看看
	function unkankanselected()
	{
		$this->common();
		if (isset($this->param["delst"])) {
			$delstr = $this->param["delst"];
		}
		if(!isset($delstr)) redirect($this->config->item('admin_path')."/site/index");
		$this->sm->kankanselected($delstr, 0);
		admin_show_success("取消推荐成功", $this->base_url);	
	}
	
	//选择取消推荐
	function unrecommendselectedstatic()
	{
		$this->common();
		if (isset($this->param["delst"])) {
			$delstr = $this->param["delst"];
		}
		if(!isset($delstr)) redirect($this->config->item('admin_path')."/site/index");
		$this->sm->recommendselectedstatic($delstr,'all', 0);
		admin_show_success("取消推荐成功", $this->base_url);	
	}
	
	//选择取消推荐
	function unbestrecommendselected()
	{
		$this->common();
		if (isset($this->param["delst"])) {
			$delstr = $this->param["delst"];
		}
		if(!isset($delstr)) redirect($this->config->item('admin_path')."/site/index");
		$this->sm->bestrecommendselected($delstr, 0);
		admin_show_success("取消推荐成功", $this->base_url);	
	}
	
	//选择删除粉丝
	function delselectedfans()
	{
		$siteid = u(4);
		if(!$delstr = u(5)) redirect($this->config->item('admin_path')."/site/fans/".$siteid);
		$this->ssm->delselectedfans($siteid, $delstr);
		admin_show_success("删除成功", $this->config->item('admin_path')."/site/fans/".$siteid);	
	}
	
	//删除粉丝
	function fansdelete()
	{
		$siteid = u(4);
		$userid = u(5);
		$this->ssm->funs_unrecord($siteid, $userid);
		admin_show_success("删除成功", $this->config->item('admin_path')."/site/fans/".$siteid);	
	}
	
	//删除小站
	function delete()
	{
		$siteid = u(4);
		$this->sm->delete($siteid);
		admin_show_success("删除成功", $this->config->item('admin_path')."/site/index");	
	}
	
	//删除选择小站
	function delselected()
	{
		if(!$delstr = u(4)) redirect($this->config->item('admin_path')."/site/index");
		$this->sm->delselected($delstr);
		admin_show_success("删除成功", $this->config->item('admin_path')."/site/index");	
	}
	
	//查看
	function show()
	{
		$siteid = u(4);
		$row = $this->sm->get($siteid);
		$viewdata['viewpath'] = $this->viewpath;
		$viewdata['row'] = $row;
		$this->load->view($this->viewpath.'/show', $viewdata);
	}
	
}

/* End of file */